package site.lanmushan.framework.datascope.interceptor;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;
import org.springframework.beans.factory.annotation.Autowired;
import site.lanmushan.framework.datascope.constant.DataControlType;
import site.lanmushan.framework.datascope.exception.DataControlException;
import site.lanmushan.framework.datascope.support.DataControlUserContextProvider;
import site.lanmushan.framework.datascope.support.DataScopeConditionProvider;
import site.lanmushan.framework.datascope.support.ISlashDataControl;
import site.lanmushan.framework.datascope.support.SlashDataControlInterceptor;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Administrator
 */
@Data
@Slf4j
public abstract class AbstractISlashDataControl implements ISlashDataControl {
    @Autowired
    private DataScopeConditionProvider dataScopeConditionProvider;
    @Autowired
    private DataControlUserContextProvider dataControlUserContextProvider;
    /**
     * 查询拦截器
     */
    protected List<SlashDataControlInterceptor> selectInterceptorList = new CopyOnWriteArrayList<>();
    /**
     * 更新拦截器
     */
    protected List<SlashDataControlInterceptor> updateInterceptorList = new CopyOnWriteArrayList<>();

    /**
     * 插入拦截器
     */
    protected List<SlashDataControlInterceptor> insertInterceptorList = new CopyOnWriteArrayList<>();

    /**
     * 插入拦截器
     */
    protected List<SlashDataControlInterceptor> deleteInterceptorList = new CopyOnWriteArrayList<>();

    @Override
    public void registerInterceptor(String sqlType, SlashDataControlInterceptor interceptor) {
        switch (sqlType) {
            case DataControlType.SELECT: {
                selectInterceptorList.add(interceptor);
                break;
            }
            case DataControlType.UPDATE: {
                updateInterceptorList.add(interceptor);
                break;
            }
            case DataControlType.DELETE: {
                deleteInterceptorList.add(interceptor);
                break;
            }
            case DataControlType.INSERT: {
                insertInterceptorList.add(interceptor);
                break;
            }
            default: {
                throw new DataControlException("未找到对应的注册类型");
            }
        }
    }

    @Override
    public String doSql(String sql) throws JSQLParserException {
        Statement statement = CCJSqlParserUtil.parse(sql);
        return doStatement(statement).toString();
    }

    @Override
    public Statement doStatement(Statement statement) throws JSQLParserException {
        if (statement instanceof Select) {
            return doSelect(statement);
        } else if (statement instanceof Update) {
            return doUpdate(statement);
        } else if (statement instanceof Delete) {
            return doDelete(statement);
        } else if (statement instanceof Insert) {
            return doInsert(statement);
        }
        return statement;
    }


}
